package ch.kerbtier.malurus.coreimpl;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ch.kerbtier.malurus.Component;
import ch.kerbtier.malurus.RenderProvider;
import ch.kerbtier.malurus.Renderer;
import ch.kerbtier.malurus.RequestContext;

public class CiRequestContext implements RequestContext {

	private HttpServletRequest request;
	private HttpServletResponse response;
	private PrintWriter writer;
	private RenderProvider baseRenderProvider = null;

	public CiRequestContext(HttpServletRequest request, HttpServletResponse response) {
		this.request = request;
		this.response = response;
		try {
			this.writer = response.getWriter();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public PrintWriter getWriter() {
		return writer;
	}

	protected void setBaseRenderProvider(RenderProvider baseRenderProvider) {
		this.baseRenderProvider = baseRenderProvider;
	}

	public void doRenderCycle() {
		
	}

	public int getRenderCycles() {
		return 0;
	}

	public void setWriter(PrintWriter writer) {
		this.writer = writer;
	}

	public void render(Component component) {
		Renderer renderer = baseRenderProvider.getRenderer(component.getClass().getName(), component.getRealisation());
		if(renderer != null) {
			renderer.render(component, this);
		}
	}

}
